package com.yiwei.hdb.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.yiwei.hdb.R;
import com.yiwei.hdb.adapter.base.ItemAdapter;
import com.yiwei.hdb.model.RepayRecordModel;
import com.yiwei.hdb.util.DateTime;
import com.yiwei.hdb.util.GlideUtils;
import com.yiwei.hdb.util.ImageUtil;
import com.yiwei.hdb.util.ToastUtil;
import com.yiwei.hdb.view.Dialog;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xsooy-pc on 2017/8/30.
 */

public class RepayRecordNewAdapter extends ItemAdapter<RepayRecordModel.Data, RepayRecordNewAdapter.RepayRecordViewHolder> {


    private Context context;

    public RepayRecordNewAdapter(Context context) {
        super(context);
        this.context = context;

    }

    @Override
    public RepayRecordViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RepayRecordViewHolder(inflate(R.layout.repay_list_items, parent));
    }

    @Override
    public void onBindViewHolder(RepayRecordViewHolder holder, int position) {
        RepayRecordModel.Data mode = getItem(position);


        if (mode.online_pay == 1) {
            holder.repayVImage.setVisibility(View.INVISIBLE);
//            1：线上支付
            if (mode.is_paid == 1) {
//                1：成功
//                view_fail_bg
//                view_success_bg
//                view_ing_bg
//                view_cancel_bg
                holder.tvCheckCancel.setBackgroundResource(R.drawable.view_success_bg);
                holder.txtOfflineStatus.setTextColor(0xff6ccd3b);
                holder.txtOfflineStatus.setText("已通过");
                holder.textView5.setText("实收金额");
            } else {
//                其他：失败
//                Glide.with(context).load(R.drawable.view_cancel_bg).into(holder.tvCheckCancel);
                holder.tvCheckCancel.setBackgroundResource(R.drawable.view_cancel_bg);
                holder.txtOfflineStatus.setTextColor(0xff888888);
                holder.txtOfflineStatus.setText("已取消");
                holder.textView5.setText("待收金额");
            }

        } else {
//            线下支付

            if (TextUtils.isEmpty(mode.getPhoto())) {
                holder.repayVImage.setVisibility(View.GONE);
            } else {
                holder.repayVImage.setVisibility(View.VISIBLE);

                GlideUtils.loadImageRound(context, holder.repayVImage, mode.getPhoto());
//                Glide.with(context).load(mode.getPhoto()).asBitmap().into(new BitmapImageViewTarget(holder.repayVImage) {
//                    @Override
//                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//                        super.onResourceReady(resource, glideAnimation);
//                        holder.repayVImage.setImageBitmap(ImageUtil.getRoundCornerImage(resource, 10, ImageUtil.HalfType.TOP));
//                    }
//                });
                holder.repayVImage.
                        setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (mode.getPhoto() != null) {
                                    Dialog.showRecordImage(context, mode.getPhoto());
                                } else {
                                    ToastUtil.makeText(context, "暂无凭证图片", 2).show();
                                }
                            }
                        });
            }
//                view_fail_bg
//                view_success_bg
//                view_ing_bg
//                view_cancel_bg
//           //订单状态：0 - 初始化、1 - 审核拒绝、2 - 支付失败、3 - 审核中、 4 - 审核通过
            if (mode.order_status == 0) {//审核中
                holder.tvCheckCancel.setBackgroundResource(R.drawable.view_ing_bg);
                holder.txtOfflineStatus.setTextColor(0xfff9a016);
                holder.txtOfflineStatus.setText("审核中");
                holder.textView5.setText("待收金额");
            } else if (mode.order_status == 1) {//审核失败
                holder.tvCheckCancel.setBackgroundResource(R.drawable.view_fail_bg);
                holder.txtOfflineStatus.setTextColor(0xfff86667);
                holder.txtOfflineStatus.setText("未通过");
                holder.textView5.setText("待收金额");
            } else if (mode.order_status == 2) {//审核成功
                holder.tvCheckCancel.setBackgroundResource(R.drawable.view_success_bg);
                holder.txtOfflineStatus.setTextColor(0xff6ccd3b);
                holder.txtOfflineStatus.setText("已通过");
                holder.textView5.setText("实收金额");
            }
        }
//else if( mode.order_status == 2){
//            holder.tvCheckCancel.setBackgroundResource(R.drawable.view_cancel_bg);
//            holder.txtOfflineStatus.setTextColor(0xff888888);
//            holder.txtOfflineStatus.setText("已取消");
//        }
        holder.date.setText(DateTime.toDateTime(mode.getCreate_time() * 1000));

        holder.txtOfflinePaymoney.setText("￥" + mode.repay_money.replace(".00", ""));

        holder.txtOfflineShishou.setText("￥" + mode.money.replace(".00", ""));

        holder.txtOfflineDikou.setText("￥" + mode.offset_money.replace(".00", ""));

        holder.txtOfflineYouhui.setText("￥" + mode.coupon_money.replace(".00", ""));


        holder.type.setText(mode.payment_name);

        if (!TextUtils.isEmpty(mode.failed_reason)) {
            holder.txtFailReason.setVisibility(View.VISIBLE);
            holder.txtFailReason.setText("失败原因：" + mode.failed_reason);
        } else {
            holder.txtFailReason.setVisibility(View.GONE);
        }
    }

    class RepayRecordViewHolder extends RecyclerView.ViewHolder {
        //        @BindView(R.id.tvCheckFailed)
//        View tvCheckFailed;
//        @BindView(R.id.tvhaveCheck)
//        View tvhaveCheck;
//        @BindView(R.id.tvCheckIng)
//        View tvCheckIng;
        @BindView(R.id.tvCheckCancel)
        ImageView tvCheckCancel;
        @BindView(R.id.type)
        TextView type;
        @BindView(R.id.txt_offline_paymoney)
        TextView txtOfflinePaymoney;
        //        @BindView(R.id.textView5)
//        TextView textView5;
        @BindView(R.id.txt_offline_shishou)
        TextView txtOfflineShishou;
        @BindView(R.id.txt_offline_dikou)
        TextView txtOfflineDikou;
        //        @BindView(R.id.textView4)
//        TextView textView4;
        @BindView(R.id.txt_offline_youhui)
        TextView txtOfflineYouhui;
        @BindView(R.id.date)
        TextView date;
        //        @BindView(R.id.linearLayout)
//        LinearLayout linearLayout;
        @BindView(R.id.txt_offline_status)
        TextView txtOfflineStatus;
        @BindView(R.id.repay_v_image)
        ImageView repayVImage;
        //        @BindView(R.id.layout_offline)
//        LinearLayout layoutOffline;
        @BindView(R.id.txt_fail_reason)
        TextView txtFailReason;
        @BindView(R.id.textView5)
        TextView textView5;
//        @BindView(R.id.repay_list_item)
//        LinearLayout repayListItem;

        public RepayRecordViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
